package io.kiki.sba.registry.common.model.slot.filter;

import com.google.common.base.Objects;
import io.kiki.sba.registry.common.model.constants.MultiValueConstants;
import lombok.NonNull;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.Set;


public class SyncSlotDataInfoIdAcceptor implements SyncSlotAcceptor {

    private final String NAME = MultiValueConstants.SYNC_SLOT_DATAINFOID_ACCEPTOR;

    private final Set<String> accepts;

    private final Set<String> filters;

    public SyncSlotDataInfoIdAcceptor(Set<String> accepts) {
        this(accepts, Collections.EMPTY_SET);
    }

    public SyncSlotDataInfoIdAcceptor(Set<String> accepts, Set<String> filters) {
        this.accepts = accepts;
        this.filters = filters;
    }

    @Override
    public boolean accept(@NonNull SyncAcceptorRequest request) {

        if (CollectionUtils.isEmpty(accepts) || StringUtils.isEmpty(request.getDataInfoId())) {
            return false;
        }

        return accepts.contains(MultiValueConstants.SYNC_ACCEPT_ALL) || accepts.contains(request.getDataInfoId());
    }

    @Override
    public boolean filterOut(@NonNull SyncAcceptorRequest request) {


        if (CollectionUtils.isEmpty(filters) || StringUtils.isEmpty(request.getDataInfoId())) {
            return false;
        }

        return filters.contains(request.getDataInfoId());
    }

    @Override
    public String name() {
        return NAME;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        SyncSlotDataInfoIdAcceptor that = (SyncSlotDataInfoIdAcceptor) o;
        return Objects.equal(NAME, that.NAME);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(NAME);
    }


}
